import axios from "axios";
import { ElMessage } from "element-plus";
import router from "@/router";
const request = axios.create({
	baseURL: "http://localhost:8121",
	timeout: 5000
});

// 添加请求拦截器
request.interceptors.request.use(
	function (config) {
		// 在发送请求之前做些什么
		config.withCredentials = true;

		return config;
	},
	function (error) {
		// 对请求错误做些什么
		return Promise.reject(error);
	}
);

// 添加响应拦截器
request.interceptors.response.use(
	res => {
		res.data;
		// console.log(res.data?.code);
		if (res.data?.code === 40100) {
			router.push("/user/login");
		}
		return res.data;
	},
	e => {
		// 统一错误提示
		ElMessage({
			type: "warning",
			message: e.response.data.message
		});
		return Promise.reject(e);
	}
);

export default request;
